/**
 * 
 */
package edu.ou.cs.youming.jaxb.types;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Youming Lin
 * 
 */
@XmlRootElement(name = "Dictionary")
public final class Dictionary {
	@XmlElement(name = "values")
	public final HashMap<String, Double> sentimentValues = new HashMap<String, Double>();

	public void addInstance(final String word, final Label label) {
		if (word == null) {
			throw new NullPointerException("word");
		}

		if (label == null) {
			throw new NullPointerException("label");
		}

		Double value = sentimentValues.get(word);

		if (value == null) {
			value = 0d;
		}

		switch (label) {
			case NEGATIVE:
				value--;
				break;
			case POSITIVE:
				value++;
				break;
			default:
				break;
		}

		sentimentValues.put(word, value);
	}

	public double getValue(final String word) {
		if (word == null) {
			throw new NullPointerException("word");
		}

		if (sentimentValues.get(word) == null) {
			throw new IllegalArgumentException("word not found in dictionary");
		}

		return sentimentValues.get(word);
	}

	public void trim(final double threshold) {
		for (final Iterator<Entry<String, Double>> itr = sentimentValues.entrySet()
				.iterator(); itr.hasNext();) {
			if (Math.abs(itr.next().getValue()) < threshold) {
				itr.remove();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Dictionary [sentimentValues=" + sentimentValues + "]";
	}
}